package com.zero.yygh.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zero.yygh.common.exception.CommonException;
import com.zero.yygh.common.result.ResultCodeEnum;
import com.zero.yygh.hosp.mapper.HospitalSetMapper;
import com.zero.yygh.hosp.service.HospitalSetService;
import com.zero.yygh.model.hosp.HospitalSet;
import com.zero.yygh.vo.order.SignInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.CookieManager;

/**
 * @author zero
 * @desc 医院设置服务实现
 * @create 2021/3/14
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService{

    @Resource
    private HospitalSetMapper hospitalSetMapper;

    /**
     * 获取医院签名Key
     * @param hoscode 医院编号
     * @return java.lang.String
     * @author zero
     * @date 2021/3/27 15:34
     */
    @Override
    public String getSignKey (String hoscode) {
        HospitalSet hospitalSet = this.getByHoscode(hoscode);
        if(null == hospitalSet) {
            throw new CommonException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if(hospitalSet.getStatus() == 0) {
            throw new CommonException(ResultCodeEnum.HOSPITAL_LOCK);
        }
        return hospitalSet.getSignKey();

    }

    /**
     * 获取医院签名信息
     * @param hoscode 医院编号
     * @return com.zero.yygh.vo.order.SignInfoVo
     * @author zero
     * @date 2021/5/8 21:32
     */
    @Override
    public SignInfoVo getSignInfoVo (String hoscode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(queryWrapper);
        if (null == hospitalSet) {
            throw new CommonException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }

    /**
     * 获取医院设置信息
     * @author zero
     * @date 2021/3/27 15:36
     * @param hoscode 医院编号
     * @return com.zero.yygh.model.hosp.HospitalSet
     */
    private HospitalSet getByHoscode(String hoscode) {
        return hospitalSetMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
    }

}
